import {encrypt, decrypt} from "@/utils/AES";

// 存储数据，key,value形式
const setStorage = (key: string, val: any) => {
    localStorage.setItem (key, encrypt (JSON.stringify(val)))
}
//根据【key】获取数据
const getStorage = (key: string) => {
    //判断存不存在,不存在直接返回null
    if (!localStorage.getItem (key)) {
        return null
    }
    return JSON.parse (decrypt (<string>localStorage.getItem (key)))
}
//根据[key]删除对应数据
const clearStorage = (key: string) => {
    localStorage.removeItem (key)
}
//删除所有缓存数据
const clearAll = () => {
    localStorage.clear ()
}

export {
    setStorage,
    getStorage,
    clearStorage,
    clearAll
}
